
//搜索用的数据
    var DATA = {

        is_online : '',

        is_pos : '',

        vendor_type : '',

        page : 1,

        per_page : 10,

        machine_code : '',

        city : '',

        province : '',

        store_id : '',

        district : '',

        customer_id : ''

    };

//vue的data
    var _Global_Data = {

        pos_status_map : {

            0 : '工作异常',

            1 : '正常工作',

            2 : '缺纸'

        },                      //小票机状态映射表

        online_status_map : {

            0 : '不在线',

            1 : '正常'

        },                      //工作状态映射表

        query_info : DATA,      //查询所需要的数据

        search_info : {},       //用于接受查询数据

        subordinates : {},      //本地存储的下级信息

        machine : {}            //机器状态信息

    };

    var MachineResource = new Resource( API_LIST.MACHINE_QUERY , Vue.http );

    !function machineInfo() {

        var vm = new Vue({

            el : '#machine_table',

            data : _Global_Data,

            created : function(){

                this.initSearchData()
                    .then(this.requestTableData(this.query_info));

            },

            computed : {

            },

            methods : {
                //select2获取value的方法
                getSelectValue : function getSelectValue(e){

                    this.query_info[e.target.dataset.name] = e.target.value;

                },
                //改变页数的时候触发的函数
                pageChange : function pageChange(val){

                    this.search_info.page = val;

                    this.search_info.per_page = this.query_info.per_page;

                    this.requestTableData(this.search_info);

                },
                //改变每页显示数时触发的函数
                sizeChange : function sizeChange(val){

                    this.search_info.per_page = val;

                    this.query_info.per_page = val;

                    this.requestTableData(this.search_info);

                },
                //点击搜索时触发的函数
                confirmSearch : function confirmSearch(){

                    var json_object = JSON.stringify(this.query_info);

                    this.search_info = JSON.parse(json_object);

                    this.requestTableData(this.query_info);

                },
                //点击跳转到货道状态页面
                toAisleStatus : function toAisleStatus( code ){

                    window.open( API_LIST.TO_AISLE + '#?machine_code=' + code );

                },
                //点击跳转到机器日志页面
                toMachineLog : function toMachineLog( code ){

                    window.open( API_LIST.TO_MACHINE_LOG + '#?machine_code=' + code );

                },
                //点击导出excel表
                getExcel : function getExcel(){

                    window.location.href = API_LIST.MACHINE_EXCEL + '?' + this.toQuery(this.search_info);

                },
                //参数拼接到网址里的方法
                toQuery : function( param_obj ) {

                    if ( ! param_obj ) return '';

                    var filter_keys_arr = Object.keys( param_obj );

                    return filter_keys_arr.reduce( function( before, current , index ) {

                        // console.log(before + ',' + current + ',' + index);

                        var joint_symbol = filter_keys_arr.length - 1 - index  ? '&' : '' ;

                        if ( param_obj[current] ) {
                            return  before + current + '=' + param_obj[current] + joint_symbol;
                        } else {
                            return before
                        }

                    }, '?' );
                },
                //初始化搜索框所需数据
                initSearchData : function initSearchData(){

                    var self = this;

                    return MachineResource.create()
                                        .then(function(res){

                                            if( parseInt(res.data.code) === 0 ){

                                                self.subordinates = res.data.subordinates;

                                            }else {

                                                alert(res.data.error);

                                            }

                                        })
                                        .catch(function(err){

                                            console.log(err);

                                            alert('请求异常，请重试！')

                                        })
                },
                //请求表格数据函数
                requestTableData : function requestTableData( params ){

                    var self = this;

                    App.blockUI("#query_form");

                    MachineResource.index( params )
                                    .then(function(res){

                                        if( parseInt(res.data.code) === 0 ){

                                            self.machine = res.data.machines;

                                            self.machine.per_page = parseInt(self.machine.per_page);

                                            App.unblockUI("#query_form");

                                        }else {

                                            App.unblockUI("#query_form");

                                            alert(res.data.error);

                                        }

                                    })
                                    .catch(function(err){

                                        console.log(err);

                                        App.unblockUI("#query_form");

                                        alert('初始化数据失败，请刷新重试！')

                                    })

                }

            }

        });

        vm.$refs.province.onchange = function(){

            if( this.value == '' ){

                _Global_Data.query_info.province = '';

            }else {

                _Global_Data.query_info.province = this.options[this.selectedIndex].text;

            }

        };

        vm.$refs.city.onchange = function(){

            if( this.value == '' ){

                _Global_Data.query_info.city = '';

            }else {

                _Global_Data.query_info.city = this.options[this.selectedIndex].text;

            }

        };

        vm.$refs.district.onchange = function(){

            if( this.value == '' ){

                _Global_Data.query_info.district = '';

            }else {

                _Global_Data.query_info.district = this.options[this.selectedIndex].text;

            }

        };

    }();